package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Activity;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.ActivityModel;

/**
 * Created by Lix on 2018/10/17.
 */

public class ActivityDetailsActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView act_name,act_intro,act_time,act_price,act_price1,act_address,act_tel,flag;
    private ImageView act_img,back,act_assint,act_collection;
    private Button signup;
    private SharedPreferences sp;
    private Context mContext;
    private int aid,uid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_details);
        initViews();
        initEvent();
        sp = getSharedPreferences("userinfo",MODE_PRIVATE);
        aid = getIntent().getIntExtra("act_id",1);
        uid = sp.getInt("uid",4);
        ActivityModel activityModel = new ActivityModel();
        activityModel.getActivity(aid,listener);
        activityModel.isCollection(aid,uid,iscollectionlistener);
    }

    //0已收藏 1未收藏
    TListener<Success> iscollectionlistener = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            if(success.getError().equals("0")){
                act_collection.setImageResource(R.drawable.ic_ycollection);
                flag.setText("1");
            }else if(success.getError().equals("1")){
                act_collection.setImageResource(R.drawable.ic_nocollection);
                flag.setText("0");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    TListener<Success> collectionlis = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            Toast.makeText(ActivityDetailsActivity.this,"收藏成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    TListener<Success> uncollectionListener = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            Toast.makeText(ActivityDetailsActivity.this,"取消收藏成功！", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    TListener<Activity> listener = new TListener<Activity>() {
        @Override
        public void onResponse(Activity activity) {
            act_name.setText(activity.getName());
            act_intro.setText(activity.getIntro());
            act_time.setText(activity.getTime());
            act_price.setText(Integer.toString(activity.getPrice()));
            act_price1.setText("￥"+Integer.toString(activity.getPrice())+"/人");
            act_address.setText(activity.getAddress());
            act_tel.setText(Integer.toString(activity.getTel()));
            Picasso.with(ActivityDetailsActivity.this)
                    .load(Server.PIC_URL_J+activity.getPic())
                    .placeholder(R.drawable.ic_default_img)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(act_img);


        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void initViews(){
        act_name = (TextView)findViewById(R.id.act_name);
        act_intro = (TextView)findViewById(R.id.act_intro);
        act_time = (TextView)findViewById(R.id.act_time);
        act_address = (TextView)findViewById(R.id.act_address);
        act_price = (TextView)findViewById(R.id.act_price);
        act_price1 = (TextView)findViewById(R.id.act_price1);
        act_tel = (TextView)findViewById(R.id.act_tel);
        act_img = (ImageView)findViewById(R.id.act_img);
        back = (ImageView) findViewById(R.id.act_back);
      //  act_assint = (ImageView) findViewById(R.id.train_assint);
        act_collection = (ImageView)findViewById(R.id.act_collection);
        signup = (Button)findViewById(R.id.act_signup);
        flag = (TextView)findViewById(R.id.flag_activity_collection);
    }

    private void initEvent(){
        back.setOnClickListener(this);
        signup.setOnClickListener(this);
        //tr_assint.setOnClickListener(this);
        act_collection.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.act_back:
                finish();
                break;
            case  R.id.act_signup:
                Intent intent = new Intent(ActivityDetailsActivity.this,ActivitySignUpActivity.class);
                intent.putExtra("act_id",aid);
                startActivity(intent);
                break;
            case R.id.act_collection:
                ActivityModel activityModel = new ActivityModel();
                if(flag.getText().equals("0")){
                    activityModel.collection(aid,uid,collectionlis);
                    act_collection.setImageResource(R.drawable.ic_ycollection);
                }else if(flag.getText().equals("1")){
                    activityModel.unCollection(aid,uid,uncollectionListener);
                    act_collection.setImageResource(R.drawable.ic_nocollection);
                }
                break;
        }

    }
}
